<?php
declare (strict_types=1);

namespace app\common\listener;

use app\common\model\User;
use think\Exception;
use think\facade\Log;
use app\common\facade\Jpush;

class AssetsJpush
{
    /**
     * 推送
     * @param $data
     * @throws Exception
     */
    public function handle($data)
    {
        try {
            $assets = [
                'currency' => get_pay_way('currency'),
                'red_coin' => get_pay_way('red_coin'),
                'chain_bean' => get_pay_way('chain_bean'),
                'love_bean' => get_pay_way('love_bean'),
                'vip_integ' => get_pay_way('vip_integ'),
                'rabbit_coupon' => get_pay_way('rabbit_coupon'),
                'ranch_points' => get_pay_way('ranch_points'),
                'ranch_bonus' => get_pay_way('ranch_bonus'),
                'subsidy' => get_pay_way('subsidy')
            ];

            $account = isset($assets[$data['account']]) ? $assets[$data['account']] : '';
            $registration_id = User::where('id', $data['uid'])->value('registration_id');
            $content = "你有新的{$account}奖励已到账！";
            $parameter = ['event_type' => 1003, 'sub_event_type' => 1100];
            $extra = ['extras' => ['parameter' => json_encode($parameter)]];

            Jpush::sendSingleNotify($registration_id, $content, $extra, $data['uid']);
        } catch (\Exception $e) {
            Log::write("listener.Jpush:" . $e->getMessage());
            throw new Exception("加载失败：" . $e->getMessage(), -1);
        }
    }
}